package com.hbusa.cotroller.chapter;

import com.hbusa.dto.TbChapter;
import com.hbusa.service.impl.*;
import com.hbusa.service.service.impl.ChapterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Sandwich
 * @version 1.0
 * @date 2021/12/27-20:27
 */
@WebServlet("/doChapterAdd")
public class chapterAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");

        ChapterServiceImpl chapterService = new ChapterServiceImpl();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        HttpSession session = req.getSession();

        long novelId1 = (long)session.getAttribute("novelId");
        session.setAttribute("chapter",chapterService.queryChapterByNovelId(novelId1));
        System.out.println(session.getAttribute("chapter"));


        System.out.println(1);

        int  chapterId = Integer.parseInt(req.getParameter("chapterId"));
        String chapterName = req.getParameter("chapterName");
        String chapterNum = req.getParameter("chapterNum");
        String content = req.getParameter("content");
        int novelId = Integer.parseInt(req.getParameter("novelId"));

        Date Time = null;
        try {
            Time = dateFormat.parse(req.getParameter("time"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date time = new java.sql.Date(Time.getTime());

        chapterService.save(new TbChapter(chapterId,chapterName,chapterNum,content,novelId,time));

        resp.sendRedirect("doChapterFindAll");
    }
}
